/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.celephais.objectGenerator.fileGenerators;

import com.celephais.objectGenerator.FieldDetailPanel;
import com.celephais.objectGenerator.FieldDetailPanel.FieldType;
import com.celephais.objectGenerator.ObjectGenerator;
import java.io.File;

/**
 * @author Eugene Shen
 */
public class ServerServiceGenerator extends FileGenerator {

	protected static final String SERVER_SERVICE_TEMPL_DIR = "templates" + File.separator + "serverService" + File.separator;

	private String toPersistRef = null;
	private String toPersistSimple = null;
	private String toTransRef = null;
	private String toTransSimple = null;

	public ServerServiceGenerator(ObjectGenerator parent) {
		super(parent);
	}

	@Override
	public String getTemplateDir() {
		return SERVER_SERVICE_TEMPL_DIR;
	}

	@Override
	public String getFileName(String baseClassName) {
		return baseClassName + "ServiceImpl";
	}
	
	@Override
	public String generateAdditional(String fileContents) {
		// Trans to Persist field copiers.
		String toPersist = generateToPersistFieldCopies();
		if (toPersist != null) {
			fileContents = fileContents.replaceAll("%toPersistFieldCopies%", toPersist);
		}

		// Persist to Trans field copiers.
		String toTrans = generateToTransFieldCopies();
		if (toTrans != null) {
			fileContents = fileContents.replaceAll("%toTransFieldCopies%", toTrans);
		}

		return fileContents;
	}

	@Override
	public String getOutputDir(String srcDir) {
		return srcDir + File.separator + "server" + File.separator + "service";
	}

	public String getToPersistTemplate(FieldType fieldType) {
		if (fieldType == FieldType.UNKNOWN) {
			return null;
		}
		else if (fieldType == FieldType.REF) {
			if (toPersistRef == null) {
				toPersistRef = readFile(SERVER_SERVICE_TEMPL_DIR, "toPersistRef.txt");
			}
			return toPersistRef;
		}
		else {
			if (toPersistSimple == null) {
				toPersistSimple = readFile(SERVER_SERVICE_TEMPL_DIR, "toPersistSimple.txt");
			}
			return toPersistSimple;
		}
	}

	public String getToTransTemplate(FieldType fieldType) {
		if (fieldType == FieldType.UNKNOWN) {
			return null;
		}
		else if (fieldType == FieldType.REF) {
			if (toTransRef == null) {
				toTransRef = readFile(SERVER_SERVICE_TEMPL_DIR, "toTransRef.txt");
			}
			return toTransRef;
		}
		else {
			if (toTransSimple == null) {
				toTransSimple = readFile(SERVER_SERVICE_TEMPL_DIR, "toTransSimple.txt");
			}
			return toTransSimple;
		}
	}

	protected String generateToPersistFieldCopies() {
		StringBuilder rtn = new StringBuilder();

		for (FieldDetailPanel fdp : parent.getDetailsPanel().getFields()) {
			FieldType type = fdp.getType();
			String name = fdp.getFieldName();
			String nameUpper = name.substring(0, 1).toUpperCase() + name.substring(1);
			String template = getToPersistTemplate(type);

			if (template != null) {
				template = template.replaceAll("%varNameUpper%", nameUpper);
				if (type == FieldType.REF) {
					// TODO: Replace all occurrences of %refType% with the name of the referenced type.
				}
				rtn.append(template); //.append(CRLF);
			}
			else {
				// TODO: Throw error?
			}
		}

		return rtn.toString();
	}

	protected String generateToTransFieldCopies() {
		StringBuilder rtn = new StringBuilder();

		for (FieldDetailPanel fdp : parent.getDetailsPanel().getFields()) {
			FieldType type = fdp.getType();
			String name = fdp.getFieldName();
			String nameUpper = name.substring(0, 1).toUpperCase() + name.substring(1);
			String template = getToTransTemplate(type);

			if (template != null) {
				template = template.replaceAll("%varNameUpper%", nameUpper);
				if (type == FieldType.REF) {
					// TODO: Replace all occurrences of %refType% with the name of the referenced type.
				}
				rtn.append(template); //.append(CRLF);
			}
			else {
				// TODO: Throw error?
			}
		}

		return rtn.toString();
	}

}
